
pipeline {
    options {
        timeout(time: 1, unit: 'HOURS')
    }
    agent any
//    triggers {
//        cron('0 * * * *')
//    }
//    parameters {
//        string(name: 'BRANCH', defaultValue: 'master', description: '')
//        string(name: 'CASE_PATH', defaultValue: 'test_data/', description: '测试用例文件/目录路径')
//        string(name: 'MARKERS', defaultValue: 'all', description: '测试用例标识（多个marker用逗号分隔）')
//        string(name: 'PHONE', defaultValue: '18610913026', description: '')
//        password(name: 'PASSWORD', defaultValue: 'password', description: '')
//        choice(name: 'LOG_LEVEL', choices: ['DEBUG', 'INFO', 'WARN', 'ERROR'], description: '')
//    }
    environment {
        WALLET_TEST_URL = "${WALLET_TEST_URL}"
        WUKONG_TEST_URL = "${WUKONG_TEST_URL}"
        WUKONG_M_TEST_URL = "${WUKONG_M_TEST_URL}"
        WALLET_VIPAPI_TEST_URL = "${WALLET_VIPAPI_TEST_URL}"
        TEST_IS_PROD = "YES"
        PHONE = "${params.PHONE}"
        PASSWORD = "${params.PASSWORD}"
        LOG_LEVEL = "${params.LOG_LEVEL}"
    }
    stages {
        stage("Build"){
            steps {
                script {
                    currentBuild.displayName = "#${BUILD_NUMBER} - ${params.BRANCH}"
                }
            }
        }
        stage('Run test case') {
            steps {
                timestamps {
                    echo "[用户手机号 ${params.PHONE}]"
                    echo "[执行测试用例]"
                    sh "printenv|grep -E '(" +
                            "WALLET_TEST_URL|WUKONG_TEST_URL|WUKONG_M_TEST_URL|WALLET_VIPAPI_TEST_URL|" +
                            "QINGZHOU_TEST_URL|QINGZHOU_STAFF_TEST_URL|QINGZHOU_QIANBAO_TEST_URL|QINGZHOU_DATONG_TEST_URL|" +
                            "PHONE|PASSWORD|QINGZHOU_PHONE|QINGZHOU_PASSWORD|" +
                            "TEST_IS_PROD|LOG_LEVEL|PROCESSES)' > .env"
                    sh "export PYTHONPATH=\$(pwd);python --version;paver run_api_cases -p \${CASE_PATH} -m \${MARKERS}"
                }
            }
        }
    }
    post {
        always {
            echo '[收集测试结果]'
            junit 'result_final.xml'
        }
        changed {
            echo '[发送邮件]'
            emailext(
                body: '''${SCRIPT, template="groovy-html.template"}''',
                from: env.DEFAULT_REPLYTO,
                replyTo: env.DEFAULT_REPLYTO,
                subject: "[Jenkins] Job ${JOB_NAME} - #${BUILD_NUMBER} - $currentBuild.currentResult",
                to: env.CURRENT_RECIPIENT,
                recipientProviders: [[$class: 'RequesterRecipientProvider']],
            )
        }
        failure {
            echo '[失败，发送邮件]'
            emailext(
                body: '''${SCRIPT, template="groovy-html.template"}''',
                from: env.DEFAULT_REPLYTO,
                replyTo: env.DEFAULT_REPLYTO,
                subject: "[Jenkins] Job ${JOB_NAME} - #${BUILD_NUMBER} - $currentBuild.currentResult",
                to: env.CURRENT_RECIPIENT,
                recipientProviders: [[$class: 'RequesterRecipientProvider']],
            )
        }
        unstable {
            echo '[不稳定，发送邮件]'
            emailext(
                body: '''${SCRIPT, template="groovy-html.template"}''',
                from: env.DEFAULT_REPLYTO,
                replyTo: env.DEFAULT_REPLYTO,
                subject: "[Jenkins] Job ${JOB_NAME} - #${BUILD_NUMBER} - $currentBuild.currentResult",
                to: env.CURRENT_RECIPIENT,
                recipientProviders: [[$class: 'RequesterRecipientProvider']],
            )
        }
    }
}